package com.tsyun.view_project;

import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.TextWatcher;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ImageSpan;
import android.util.TypedValue;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class TextViewActivity extends AppCompatActivity {
     TextView textView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_textview);
        TextView textView =  findViewById(R.id.tv_desc);
        EditText etInput = findViewById(R.id.et_input);
        TextView tvCount = findViewById(R.id.tv_count);
        etInput.addTextChangedListener(new TextWatcher() {
            //在文本输入改变之前的方法
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            //文本输入的时候
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            //文本输入之后
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length()>100){
                    Toast.makeText(TextViewActivity.this,"您已输入最大文字限制",Toast.LENGTH_SHORT).show();
                    return;
                }
                tvCount.setText(s.length()+"/100");
            }
        });
        RadioButton rb = new RadioButton(this);
        //通过资源文件的方式来设置颜色
//        textView.setTextColor(getResources().getColor(R.color.purple_200));
        //第一种 Color.RED 第二种Color.rgb Color.argb 第三种 Color.parseColor("#颜色值")
//        textView.setTextColor(Color.rgb(100,120,210));
//        textView.setTextColor(Color.argb(100,120,210,30));
//        textView.setTextColor(Color.parseColor("#D7E6FF"));
        textView.setTextColor(Color.BLACK);
        CharSequence textValue = textView.getText();
        textView.setTextSize( TypedValue.COMPLEX_UNIT_SP,15);
        textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD_ITALIC));
        //可扩展的字符串
        SpannableString spannableString = new SpannableString(textValue);
        ImageSpan imageSpan = new ImageSpan(this,R.mipmap.ic_question);
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                Toast.makeText(TextViewActivity.this,"点击了连接",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(false);
                ds.linkColor = Color.TRANSPARENT;
            }
        };
        spannableString.setSpan(imageSpan,textValue.toString().indexOf("（")-1,textValue.toString().indexOf("（"),SpannableString.SPAN_INCLUSIVE_INCLUSIVE);
        spannableString.setSpan(clickableSpan,textValue.toString().indexOf("（"),textValue.toString().indexOf("）"),SpannableString.SPAN_INCLUSIVE_INCLUSIVE);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setText(spannableString);
        ImageView ivCat = findViewById(R.id.iv_cat);
        //通过ImageBitmap的方式设置图片
//        ivCat.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.bg));
        //通过设置ImageDrawable的方式来设置图片
//        ivCat.setImageDrawable(getResources().getDrawable(R.drawable.bg));
        //通过设置imageResource的方式来设置图片源
//        ivCat.setImageResource(R.drawable.bg);
//        ivCat.setImageURI(Uri.parse("https://www.baidu.com/img/flexible/logo/pc/result.png"));
        //setImageURI只可以设置本地的图片资源
//        ivCat.setImageURI();
        //设置adjustViewBounds属性
        ivCat.setAdjustViewBounds(true);
    }


    public void onClickMe(){
        
    }
}
